package gov.fnal.xpathsearch.rest;

import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyRequest;
import com.sun.grizzly.tcp.http11.GrizzlyResponse;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
import com.sun.jersey.spi.container.servlet.ServletContainer;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

public class RunXPathSearchResource {
 public static void main0(String[] args) throws IOException {
            final String baseUri = "http://0.0.0.0:9998/";
            final Map<String, String> initParams = new HashMap<String, String>();
            String rootpath = System.getProperty("xpathsearch.home", System.getProperty("user.dir"));
            initParams.put("com.sun.jersey.config.property.packages", "gov.fnal.xpathsearch.rest");

           System.out.println("Starting grizzly...");
           SelectorThread threadSelector = GrizzlyWebContainerFactory.create(
                   baseUri, initParams);
           threadSelector.setWebAppRootPath(rootpath);
           System.out.println(String.format(
                   "Jersey app started with WADL available at %sapplication.wadl\n" +
                           "Service URI is %sxpathsearch\nHit enter to stop it...", baseUri, baseUri));
           System.in.read();
           threadSelector.stopEndpoint();
           System.exit(0);
 }
     public static void main(String[] args) throws IOException {
   // static content is linked from here
   String proxy_dir = System.getProperty("xpathsearch.webdir", System.getProperty("user.dir"));
   GrizzlyWebServer gws = new GrizzlyWebServer(9998, proxy_dir);

   // Jersey web resources
   ServletAdapter jerseyAdapter = new ServletAdapter();
   jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages", "gov.fnal.xpathsearch.rest");
   jerseyAdapter.setContextPath("/jersey");
   jerseyAdapter.setServletInstance(new ServletContainer());

   // Another non-Jersey servlet
   //ServletAdapter simpleServletAdapter = new ServletAdapter();
   //simpleServletAdapter.setContextPath("/simple");
   //simpleServletAdapter.setServletInstance(new SimpleServlet());

   // register all above defined adapters
   gws.addGrizzlyAdapter(jerseyAdapter, new String[] {"/jersey"});
   //gws.addGrizzlyAdapter(simpleServletAdapter, new String[] {"/simple"});
   gws.addGrizzlyAdapter(new GrizzlyAdapter(){
           @Override
           public void service(GrizzlyRequest arg0, GrizzlyResponse arg1) {
               System.out.println("In service.");                          }
    }, new String[] {"/"});

   // let Grizzly run
   gws.start();
     System.out.println(String.format(
           "Jersey app started\nHit enter to stop it..."));
   System.in.read();
   gws.stop();
   System.exit(0);
}
}

